UIBattlePass = Class("UIBattlePass", UIComponentBase)

function UIBattlePass:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent
    self.drawWrapper = GoWrapper.New()
    self.lihui = self:GetChild("lihui");
    self.lihui2 = self:GetChild("lihui2");
    self.tokenBtn = self:GetChild("token");
    self.title = self:GetChild("title");
    self.chooseBtn = self:GetChild("choose")
    self.classTips = self:GetChild("class")
    self.tipsTips = self:GetChild("tips")
    self.nameTips = self:GetChild("name")
    self.expTips = self:GetChild("exp")
    self.timeTips = self:GetChild("time")
    self.buyBP = self:GetChild("buyBP")
    self.lvTips = self:GetChild("lvTips")
    self.banner = self:GetChild("bannerBg")
    self.goBtn = self:GetChild("goBtn")
    self.goItem = self:GetChild("Item", self.goBtn)
    self.goTitle = self:GetChild("title", self.goBtn)
    self.buyController = self:GetController("buy")
    self.showTipsController = self:GetController("showTips")

    self.giftList = self:SetDynamicList(self:GetChild("list"),self.updateItem);

    self:RegisterOnClick(self:GetChild("buybtn"),self.ShowBuy);
    self:RegisterOnClick(self:GetChild("goBtn"),self.ClickGo);
    self:RegisterOnClick(self:GetChild("choose"),self.ClickChoose);
    self:RegisterOnClick(self:GetChild("buyBP"),self.ShowBuyBP);
    self:RegisterOnClick(self:GetChild("class"),self.ShowTips);
    self:RegisterOnClick(self:GetChild("tipsBg"),self.CloseTips);
    self:RegisterOnClick(self:GetChild("btn_QuickGet"),self.ClickOneReward);
end

function UIBattlePass:Refresh()
    logicMgr.CostManager.updateActiveOpenStatus(self.activeId);
    self.severData, self.itemTableData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    self.countDownNum = tonumber(tostring(self.severData.nextResetTime)) * 0.001 - HelperFunc.GetServerTime();
    if self.severData.buyCount <= 0 then
        self.tokenBtn:GetController("gray").selectedIndex = 1;
        self.buyController.selectedIndex = 0;
    else
        self.tokenBtn:GetController("gray").selectedIndex = 0;
        self.buyController.selectedIndex = 1;
    end
    self.showTipsController.selectedIndex = 0;
    self.lvTips.text = string.format(getLanguage(self.modelTableData.f_BpLvTips), getLanguage(self.modelTableData.f_Title), self.severData.class, getLanguage(self.modelTableData.f_Title), self.modelTableData.f_MaxLv)
    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
    self.banner.url = tostring(self.modelTableData.f_Banner);
    local colorCfg = self.banner.color;
    if self.modelTableData.f_BannerColor then
        local r, g, b, a = HelperFunc.GetColorOfRGB(self.modelTableData.f_BannerColor);
        colorCfg = Color.New(r / 255, g / 255, b / 255, a / 255);
        self.banner.color = Color.New(r / 255, g / 255, b / 255, 255);
    end
    self.parent:UpdateCostBg(tostring(self.modelTableData.f_BackGroundPic), colorCfg);
    self.tokenBtn.icon = "ui://UIBattlePass/"..self.modelTableData.f_BattlePic;
    self.chooseBtn.visible = self.severData.canChange == 1 and self.modelTableData.f_CanChange == 1 and self.severData.class >= self.modelTableData.f_ChangeLv;
    self.nameTips.text = getLanguage(self.modelTableData.f_NeedItemName);
    self.title.text = getLanguage(self.modelTableData.f_Title);
    self.expTips.text = self.severData.progress;
    if tonumber(self.modelTableData.f_NeedItem) == 0 then
        self.goBtn.visible = false;
    else
        logicMgr.CostManager.RefreshItem(self.goItem, {self.modelTableData.f_NeedItem, self.severData.progress});
    end

    self.goItem.onClick:Clear();
    self.tipsTips.text = getLanguage(self.modelTableData.f_Tips);
    self.classTips.text = "战令等级 "..self.severData.class;
    self.buyBP.visible = self.countDownNum <= self.modelTableData.f_BuyBpTime and self.severData.needBPIndex ~= nil;
    self.buyBP.title = getLanguage(self.modelTableData.f_BuyBPTips);
    if self.countDownNum <= 0 then
        self.timeTips.text = getLanguage("Sys_ActivityEnd");
    else
        self:TimeCall(self.OnCountDown, 1000, -1);
        self.timeTips.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
    end
    self.canReward = nil;
    --logicMgr.CostManager.updateActiveReceiveStatus(self.severData.items, self.activeId, {1, 4});
    self.giftList.numItems = #self.severData.items;
    HelperFunc.ListTranstionPlay(self.giftList, function ()
        self.giftList:ScrollToView(self.severData.nowCanReceiveIndex - 1, true, true);
    end);
    if #self.severData.items <= 5 then
        self.giftList.scrollPane.touchEffect = false;
    else
        self.giftList.scrollPane.touchEffect = true;
    end
    logicMgr.CostManager.updateLiHui(self, self.drawWrapper, self.lihui, self.lihui2, self.modelTableData.f_HeroSpine);
    --self:ClickCloseTips();
end

function UIBattlePass:updateItem(index, obj)
    local severData = self.severData.items[index];
    local key = severData.subId;
    local tableData = self.itemTableData[tostring(key)];
    if tableData == nil then
        obj.visible = false;
        return ;
    end
    obj.visible = true;
    local upBar = obj:GetChild("up");
    local upBG = obj:GetChild("upBG");
    local downBar = obj:GetChild("down");
    local num = obj:GetChild("title");
    local node = obj:GetChild("node"):GetController("show");
    local first = obj:GetChild("first");
    local list = obj:GetChild("list");
    local get = obj:GetChild("get");
    local BPIcon = obj:GetChild("BPIcon");
    local progressUpController = obj:GetController("progressUp");
    local progressDownController = obj:GetController("progressDown");
    local stateController = obj:GetController("state");
    num.text = string.format(getLanguage(self.modelTableData.f_NeedItemDes), tableData.f_NeedPoint);
    get.onClick:Clear();
    node.selectedIndex = 1;
    -- 0.未完成
    -- 1.未领取
    -- 2.已全部领取
    -- 3.普通已领取，高级未购买
    -- 4.普通已领取，高级已购买未领取
    if severData.receiveState == 0 then
        node.selectedIndex = 0;
        stateController.selectedIndex = 0;
    elseif severData.receiveState == 1 then
        self.canReward = true;
        if self.severData.buyCount <= 0 then
            stateController.selectedIndex = 1;
        else
            stateController.selectedIndex = 5;
        end
        get.onClick:Add(function() self:ClickReward(key); end);
    elseif severData.receiveState == 2 then
        stateController.selectedIndex = 3;
    elseif severData.receiveState == 3 then
        stateController.selectedIndex = 2;
        get.onClick:Add(function() self:ShowBuy(); end);
    elseif severData.receiveState == 4 then
        self.canReward = true;
        stateController.selectedIndex = 4;
        get.onClick:Add(function() self:ClickReward(key); end);
    end
    progressUpController.selectedIndex = 1;
    progressDownController.selectedIndex = 1;
    BPIcon.url = EnumConst.AtlasPath.ItemAtlas .. self.modelTableData.f_NeedItemIcon;
    if severData.isFrist ~= nil then
        progressUpController.selectedIndex = 0;
    elseif severData.isLast ~= nil then
        progressDownController.selectedIndex = 0;
    end
    upBar.height = severData.upNum * upBG.height * 0.01;
    downBar.height = severData.downNum * upBG.height * 0.01;
    logicMgr.CostManager.RefreshItem(first:GetChild("item"), decodeJsonStrToTabel(tableData.f_BaseReward)[1]);
    if severData.receiveState == 0 then
        first:GetController("start").selectedIndex = 0;
    elseif severData.receiveState == 1 then
        first:GetController("start").selectedIndex = 1;
    else
        first:GetController("start").selectedIndex = 2;
    end
    list:RemoveChildrenToPool()
    local itemData = decodeJsonStrToTabel(tableData.f_AdvancedReward);
    for i = 1, #itemData do
        local item = list:AddItemFromPool()
        --item:GetChild("n11"):SetSize(29, 42);
        --item:GetChild("n11"):SetXY(70, 64);
        logicMgr.CostManager.RefreshItem(item:GetChild("item"), itemData[i]);
        if severData.receiveState == 2 then
            item:GetController("start").selectedIndex = 2;
        elseif self.severData.buyCount <= 0 then
            item:GetController("start").selectedIndex = 3;
        elseif severData.receiveState == 0 then
            item:GetController("start").selectedIndex = 0;
        else
            item:GetController("start").selectedIndex = 1;
        end
    end
end

function UIBattlePass:OnCountDown()
    self.countDownNum = self.countDownNum - 1;
    if self.countDownNum <= 0 then
        self.timeTips.text = getLanguage("Sys_ActivityEnd");
        return true;
    end
    if self.countDownNum <= self.modelTableData.f_BuyBpTime and self.severData.needBPIndex ~= nil then
        self.buyBP.visible = true;
    end
    self.timeTips.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
end

function UIBattlePass:ShowBuy()
    showUI(PanelNames.UIBattlePassTips, nil, self.activeId, 1);
end

--function UIBattlePass:ClickHelp()
--    logicMgr.UIShowManager.ShowCommonTips(self.modelTableData.f_ExplainTitle, self.modelTableData.f_ExplainText);
--end
--
function UIBattlePass:ShowBuyBP()
    showUI(PanelNames.UIBattlePassTips, nil, self.activeId, 2);
end
function UIBattlePass:ShowTips()
    self.showTipsController.selectedIndex = 1;
end
function UIBattlePass:CloseTips()
    self.showTipsController.selectedIndex = 0;
end

function UIBattlePass:ClickChoose()
    showUI(PanelNames.UIBattlePassTips, nil, self.activeId, 3);
end

function UIBattlePass:ClickReward(id)
    local data = {};
    table.insert(data, id);
    logicMgr.CostManager.getRewardById(self.activeId, data, nil, self);
end

function UIBattlePass:ClickOneReward()
    if self.canReward then
        self:ClickReward(0);
    else
        UISysTips.AddMsg(getLanguage("TaskInfo_btnQFtips"));
    end
end

function UIBattlePass:ClickGo()
    if self.modelTableData.f_Switchover ~= 0 and logicMgr.FuncOpenManager.isOpen(self.modelTableData.f_Switchover, true) then
        self.parent:onBack();
        logicMgr.FuncOpenManager.jump(self.modelTableData.f_Switchover)
    end
end

-- 卸载
function UIBattlePass:OnDestroy()
    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    if self._package and UIPackage.GetById(self._package) then
        UIPackage.RemovePackage(self._package)
    end
end